﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using RM2012Server.Controller;
using RM2012Server.Model;
using RM2012Server.Business;

namespace RM2012Server.View
{
    public partial class frmSupplier : Office2007Form
    {
        #region Fields
        SupplierCtrl m_SupplierCtrl = new SupplierCtrl();
        public static bool isNew = false;
        #endregion

        #region Constructor
        public frmSupplier()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void frmSupplier_Load(object sender, EventArgs e)
        {
            isNew = false;
            m_SupplierCtrl.Show(dgvSupplier, bindingNavigatorSupplier, txtSupplierId, txtSupplierName, txtTaxCode, txtState, txtAddress, txtPhone, txtEmail, txtWebsite, txtRemark);
            SaveItem.Enabled = false;
        }
        #endregion

        #region Button Click
        private void btnDongY_Click(object sender, EventArgs e)
        {
            SupplierInfo info = new SupplierInfo(txtSupplierId.Text, txtSupplierName.Text, txtTaxCode.Text, txtState.Text, txtAddress.Text, txtPhone.Text, txtEmail.Text, txtWebsite.Text, txtRemark.Text);
            if (txtSupplierName.Text.Equals("") || txtSupplierName.Text == null)
            {
                MessageBoxEx.Show("Tên nhà cung cấp không được để trống !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (isNew == true)
                {
                    m_SupplierCtrl.Add(info);
                }
                else
                {
                    m_SupplierCtrl.Update(info);
                }
                m_SupplierCtrl.Show(dgvSupplier, bindingNavigatorSupplier, txtSupplierId, txtSupplierName, txtTaxCode, txtState, txtAddress, txtPhone, txtEmail, txtWebsite, txtRemark);
                SaveItem.Enabled = false;
                isNew = false;
            }
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_SupplierCtrl.Show(dgvSupplier, bindingNavigatorSupplier, txtSupplierId, txtSupplierName, txtTaxCode, txtState, txtAddress, txtPhone, txtEmail, txtWebsite, txtRemark);
            SaveItem.Enabled = false;
        }
        #endregion

        #region BindingNavigator Item Click
        private void AddNewItem_Click(object sender, EventArgs e)
        {
            isNew = true;
            if (dgvSupplier.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            DataRow m_Row = m_SupplierCtrl.AddNewRow();
            m_Row["SupplierId"] = GetID.Supplier(dgvSupplier);
            m_Row["SupplierName"] = "";
            m_Row["TaxCode"] = "";
            m_Row["State"] = "Đang hoạt động";
            m_Row["Address"] = "";
            m_Row["Phone"] = "";
            m_Row["Email"] = "";
            m_Row["Website"] = "";
            m_Row["Remark"] = "";
            m_SupplierCtrl.AddNewSupplier(m_Row);
            bindingNavigatorSupplier.BindingSource.MoveLast();
            SaveItem.Enabled = true;
        }

        private void RefreshItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_SupplierCtrl.Show(dgvSupplier, bindingNavigatorSupplier, txtSupplierId, txtSupplierName, txtTaxCode, txtState, txtAddress, txtPhone, txtEmail, txtWebsite, txtRemark);
            SaveItem.Enabled = false;
        }

        private void DeleteItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            if (dgvSupplier.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            else if (dgvSupplier.SelectedRows.Count > 0)
            {
                if (MessageBoxEx.Show("Bạn có chắc chắn xóa?", "XÓA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in dgvSupplier.SelectedRows)
                    {
                        bindingNavigatorSupplier.BindingSource.RemoveAt(row.Index);
                    }
                    SaveItem.Enabled = true;
                }
            }
        }

        private void SaveItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            bindingNavigatorPositionItem.Focus();
            bool check = true;
            foreach (DataGridViewRow row in dgvSupplier.Rows)
            {
                if (row.Cells["colSupplierId"].Value.ToString() == "" || row.Cells["colSupplierName"].Value.ToString() == "")
                {
                    check = false;
                }
            }
            if (check)
            {
                if (!m_SupplierCtrl.SaveData())
                {
                    MessageBoxEx.Show("Không thể thực hiện !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveItem.Enabled = false;
                }
            }
            else
            {
                MessageBoxEx.Show("Dữ liệu không hợp lệ. Mã và tên nhà cung cấp không được để trống !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void ExitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Searching
        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                if (rbSupplierId.Checked == true)
                {
                    dt = m_SupplierCtrl.GetByLikeId(txtKeyWord.Text);
                }
                else if (rbSupplierName.Checked == true)
                {
                    dt = m_SupplierCtrl.GetByName(txtKeyWord.Text);
                }
                else if (rbTaxCode.Checked == true)
                {
                    dt = m_SupplierCtrl.GetByTaxCode(txtKeyWord.Text);
                }
                else if (rbAddress.Checked == true)
                {
                    dt = m_SupplierCtrl.GetByAddress(txtKeyWord.Text);
                }
                else if (rbPhone.Checked == true)
                {
                    dt = m_SupplierCtrl.GetByPhone(txtKeyWord.Text);
                }
                else if (rbEmail.Checked == true)
                {
                    dt = m_SupplierCtrl.GetByEmail(txtKeyWord.Text);
                }
                else if (rbWebsite.Checked == true)
                {
                    dt = m_SupplierCtrl.GetByWebsite(txtKeyWord.Text);
                }
                else
                {
                    dt = m_SupplierCtrl.GetByName(txtKeyWord.Text);
                }
                m_SupplierCtrl.ShowWithSources(dgvSupplier, bindingNavigatorSupplier, txtSupplierId, txtSupplierName, txtTaxCode, txtState, txtAddress, txtPhone, txtEmail, txtWebsite, txtRemark, dt);
                SaveItem.Enabled = false;
            }
            catch
            {
                MessageBoxEx.Show("Không tìm thấy kết quả !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void rbSupplierId_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Mã nhà cung cấp :";
        }

        private void rbSupplierName_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Tên nhà cung cấp :";
        }

        private void rbTaxCode_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Mã số thuế :";
        }

        private void rbAddress_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Địa chỉ :";
        }

        private void rbPhone_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Điện thoại :";
        }

        private void rbEmail_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Hòm thư :";
        }

        private void rbWebsite_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Website :";
        }
        #endregion

        #region DataGridView Event
        private void dgvSupplier_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            SaveItem.Enabled = true;
        }
        #endregion

        #region Context Menu Event Click
        private void AddNewItemContext_Click(object sender, EventArgs e)
        {
            AddNewItem_Click(sender, e);
        }

        private void RefreshItemContext_Click(object sender, EventArgs e)
        {
            RefreshItem_Click(sender, e);
        }

        private void DeleteItemContext_Click(object sender, EventArgs e)
        {
            DeleteItem_Click(sender, e);
        }

        private void SaveItemContext_Click(object sender, EventArgs e)
        {
            SaveItem_Click(sender, e);
        }

        private void ExitItemContext_Click(object sender, EventArgs e)
        {
            ExitItem_Click(sender, e);
        }
        #endregion
    }
}
